﻿/*
 * =====================================================================================
 *
 *       Filename:  AccessDAL.cs
 *
 *    Description:  access tools
 *
 *        Version:  1.1
 *        Created:  imbedinlove@gmail.com
 *       Revision:  none
 *       Compiler:  Visual Studio
 *
 *         Author:  imbedinlove@gmail.com
 *        Company:  program4fun
 *           Date:  2013年5月23日 16:24:25
 *         Update:  2014年10月8日 17:30:57
 *
 * =====================================================================================
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Configuration;
using System.IO;

namespace ImbedLibrary
{
    #region SQLDataType
    /// <summary>
    /// Access 数据库字段数据类型
    /// </summary>
    public enum SQLDataType
    {
        Integer,
        Text,
        CHARACTER,
        VARCHAR,
        BINARY,
        BOOLEAN,
        VARBINARY,
        DATE,
        TIME
    }
    #endregion

    #region DAL
    /// <summary>
    /// 数据库操作类
    /// </summary>
    public class DAL
    {
        #region 构造函数
        /// <summary>
        /// DAL
        /// 此初始化完成后使用 Access 数据库
        /// </summary>
        /// <param name="name">table name</param>
        /// <param name="basePath">access base path</param>
        public DAL(String name, String basePath)
        {
            this.tableName = name;
            this.basePath = basePath;
            if (File.Exists(basePath))
            {
                helper = new AccessHelper(basePath);
            }
            else if (basePath.ToLower().IndexOf("initial catalog") != -1)
            {
                helper = new SqlHelper(basePath);
            }
            else
            {
                throw new Exception("E05101:因为连接字符串错误，或者Access数据库路径错误，初始化失败");
            }
        }

        /// <summary>
        /// 初始化
        /// xml配置文件中必须存在 name = AccessCon 的Access数据库连接字符串
        /// 此初始化完成后使用 Access 数据库
        /// </summary>
        /// <param name="name">表名</param>
        public DAL(String name)
        {
            this.tableName = name;
            this.basePath = String.Format(ConfigurationManager.ConnectionStrings["AccessCon"].ConnectionString, AppDomain.CurrentDomain.BaseDirectory);
            helper = new AccessHelper(basePath);
        }
        #endregion

        #region 变量
        /// <summary>
        /// AccessHelper 
        /// </summary>
        protected SQLBaseHelper helper;

        /// <summary>
        /// table name
        /// </summary>
        protected String tableName = String.Empty;

        /// <summary>
        /// access base path
        /// </summary>
        protected String basePath = String.Empty;

        /// <summary>
        /// OrderBy
        /// </summary>
        protected String orderBy = String.Empty;
        #endregion

        #region 委托方法
        /// <summary>
        /// Render
        /// 委托方法
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="list"></param>
        protected virtual void Render(IList<ArrayList> list, IDataReader reader)
        {
            ArrayList rowList;
            List<string> foledList = new List<string>();
            bool hasData = reader.Read();
            if (hasData)
                foledList = helper.GetTableField(tableName, "COLUMN_NAME");
            while (hasData)
            {
                rowList = new ArrayList();
                for (int i = 0; i < foledList.Count; i++)
                {
                    rowList.Add(reader[foledList[i]].ToString());
                }
                list.Add(rowList);
                hasData = reader.Read();
            }
        }
        #endregion

        #region Select

        /// <summary>
        /// SqlCount
        /// 条数sql语句
        /// </summary>
        /// <param name="sql"></param>
        public String SqlCount()
        {
            return "select count(*) from [" + tableName + "] ";
        }

        /// <summary>
        /// SelCount
        /// 条数
        /// </summary>
        /// <returns></returns>
        public Int32 SelCount()
        {
            try
            {
                return helper.ExcuteScalar(SqlCount());
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 表中最大ID
        /// </summary>
        /// <returns>最大ID</returns>
        public Int32 GetCount()
        {
            try
            {
                List<string> foledList = new List<string>();
                foledList = helper.GetTableField(tableName, "COLUMN_NAME");
                string sql = "select max(" + foledList[0] + ") from " + tableName;
                return helper.ExcuteScalar(sql);
            }
            catch (Exception e)
            {
                if (e.Message == "Object cannot be cast from DBNull to other types.")
                {
                    return 0;
                }
                else if (e.Message == "对象不能从 DBNull 转换为其他类型。")
                {
                    return 0;
                }
                else if (e.Message == "Format of the initialization string does not conform to the OLE DB specification.")
                    throw new Exception("与数据库的连接失败,可能是连接字符串出错了!");
                else
                {
                    throw;
                }
            }
        }

        /// <summary>
        /// SqlPage
        /// 分页sql语句
        /// </summary>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        protected String SqlPage(Int32 pageSize)
        {
            return "select top " + pageSize + " * from " + tableName;
        }

        /// <summary>
        /// 获取表字段名
        /// </summary>
        /// <param name="name">表名</param>
        /// <param name="type">COLUMN_NAME or DESCRIPTION</param>
        /// <returns></returns>
        public List<String> GetTableField()
        {
            return helper.GetTableField(this.tableName, "COLUMN_NAME");
        }

        /// <summary>
        /// 返回sql语句
        /// </summary>
        /// <returns></returns>
        protected string GetSqlStr()
        {
            return "select * from [" + tableName + "] ";
        }

        /// <summary>
        /// 返回sql语句
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string GetSqlStr(string str)
        {
            return GetSqlStr().Replace("*", str);
        }

        /// <summary>
        /// 返回sql语句
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <returns>sql语句</returns>
        private string GetSqlStr(ArrayList field, ArrayList data)
        {
            String sqlStr = GetSqlStr() + " where ";
            return GetStringSql(field, data, ref sqlStr);
        }

        /// <summary>
        /// 返回查询时间段sql语句
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <param name="sqlStr"></param>
        /// <returns></returns>
        private string GetDataSql(string field, ArrayList data, string orderby)
        {
            if (data.Count != 2)
            {
                return null;
            }
            return GetSqlStr() + " where [" + field + "] between #" + data[0] + "# and #" + data[1] + "# order by " + orderby;
        }

        /// <summary>
        /// 返回字符条件sql语句
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <param name="sqlStr">sql语句</param>
        /// <returns>sql语句</returns>
        private string GetStringSql(ArrayList field, ArrayList data, ref String sqlStr)
        {
            if (field.Count != data.Count)
            {
                return null;
            }
            for (int i = 0; i < field.Count; i++)
            {
                sqlStr += field[i] + data[i].GetData();
                if (i + 1 != field.Count)
                    sqlStr += " and ";
            }
            return sqlStr;
        }

        /// <summary>
        /// 查询表内所有数据
        /// </summary>
        /// <returns></returns>
        public IList<ArrayList> SelAll()
        {
            return SelAll<ArrayList>(null, Render);
        }

        public IList<T> SelAll<T>(IDataRender<T> reader)
        {
            return SelAll<T>(null, reader);
        }

        /// <summary>
        /// 查询表中指定条数数据
        /// </summary>
        /// <param name="top">条数</param>
        /// <param name="order">排序</param>
        /// <returns></returns>
        public IList<ArrayList> SetTop(string top, string order)
        {
            string sql = GetSqlStr(top) + " " + order;
            return Sel(sql);
        }

        /// <summary>
        /// SelAll 
        /// 查询表内所有数据
        /// </summary>
        /// <param name="order">排序字段</param>
        /// <returns></returns>
        public IList<ArrayList> SelAll(string order)
        {
            return SelAll<ArrayList>(order, Render);
        }

        /// <summary>
        /// 查询表内所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="order"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public IList<T> SelAll<T>(string order, IDataRender<T> reader)
        {
            string sql = GetSqlStr() + (order == null ? "" : " order by " + order);
            return Sel(sql, reader);
        }

        public IList<ArrayList> SelAll(string order, string name)
        {
            string sql = GetSqlStr() + " order by [" + order + "]";
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql, Render);
            return list.Count == 0 ? null : list;
        }

        public IList<ArrayList> ExecuteReader(string sql)
        {
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql, Render);
            return list.Count == 0 ? null : list;
        }

        /// <summary>
        /// SelFuzzy
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public IList<ArrayList> SelFuzzy(string field, string data, string order)
        {
            string sql = GetSqlStr() + " where [" + field + "] like '%" + data + "%' order by " + order;
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql, Render);
            return list.Count == 0 ? null : list;
        }

        /// <summary>
        /// SelFuzzy
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public IList<ArrayList> SelFuzzy(ArrayList field, ArrayList data, string order)
        {
            string sql = GetSqlStr() + " where ";
            if (field.Count == data.Count)
            {
                for (int i = 0; i < field.Count; i++)
                {
                    sql += field[i] + " like '%" + data[i] + "%'";
                    if (i + 1 != field.Count)
                        sql += " and ";
                }
            }
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql + " order by " + order, Render);
            return list.Count == 0 ? null : list;
        }

        /// <summary>
        /// Sel
        /// </summary>
        /// <param name="field">条件字段</param>
        /// <param name="data">条件数据</param>
        /// <param name="order">排序:ID</param>
        /// <returns></returns>
        public IList<T> Sel<T>(string field, object data, string order, IDataRender<T> reader)
        {
            string sql = GetSqlStr() + " where [" + field + "] " + data.GetData() + " order by " + order;
            return Sel(sql, reader);
        }

        /// <summary>
        /// Sel
        /// </summary>
        /// <param name="field">条件字段</param>
        /// <param name="data">条件数据</param>
        /// <param name="order">排序:ID</param>
        /// <returns></returns>
        public IList<ArrayList> Sel(string field, object data, string order)
        {
            string sql = GetSqlStr() + " where [" + field + "] " + data.GetData() + " order by " + order;
            return Sel(sql);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <returns>数据</returns>
        public IList<ArrayList> Sel(string field, string data)
        {
            string sql = GetSqlStr() + " where [" + field + "] " + data.GetData() + "";
            return Sel(sql);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public IList<ArrayList> Sel(string sql)
        {
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql.Replace("!=", "<>"), Render);
            return list.Count == 0 ? null : list;
        }

        private IList<T> Sel<T>(string sql, IDataRender<T> reader)
        {
            IList<T> result = helper.ExecuteReader(sql, reader);
            return result.Count == 0 ? null : result;
        }

        /// <summary>
        /// Sel
        /// </summary>
        /// <param name="field">条件字段</param>
        /// <param name="data">条件数据</param>
        /// <param name="order">排序:ID</param>
        /// <returns></returns>
        public IList<ArrayList> Sel(string field, Int32 data, string order)
        {
            string sql = GetSqlStr() + " where [" + field + "] = " + data + " order by " + order;
            return Sel(sql);
        }

        /// <summary>
        /// 按照时间段查询数据
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="time">起止时间</param>
        /// <returns></returns>
        public IList<T> SelForBeginAndEndTime<T>(string field, ArrayList time, IDataRender<T> reader, string orderby)
        {
            string sql = GetDataSql(field, time, orderby);
            return helper.ExecuteReader(sql, reader);
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <param name="order">排序字段</param>
        /// <returns></returns>
        public IList<ArrayList> Sel(ArrayList field, ArrayList data, String order)
        {
            string sql = GetSqlStr(field, data);
            if (sql == null)
            {
                return null;
            }
            return Sel(sql);
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <returns>DataTable</returns>
        public DataTable Sel(ArrayList field, ArrayList data)
        {
            string sql = GetSqlStr(field, data);
            if (sql == null)
            {
                return null;
            }
            return helper.ExecuteReader(sql).Tables[0];
        }

        /// <summary>
        /// Sel
        /// </summary>
        /// <param name="filed">字段</param>
        /// <param name="data">数据</param>
        /// <param name="condition">条件[=/<>]</param>
        /// <param name="joint">连接条件[and/or]</param>
        /// <param name="order">排序:ID</param>
        /// <returns></returns>
        public IList<ArrayList> Sel(ArrayList filed, ArrayList data, ArrayList condition, ArrayList joint, String order)
        {
            String sql = "select * from [" + tableName + "] where ";
            if (filed.Count == data.Count)
            {
                for (int i = 0; i < filed.Count; i++)
                {
                    String type = data[i].GetType().Name;
                    if (type == "UINT32" || type == "Int32")
                        sql += filed[i] + " " + condition[i] + " " + data[i];
                    else
                        sql += filed[i] + " " + condition[i] + " '" + data[i] + "'";
                    if (i + 1 != filed.Count)
                        sql += " " + joint[i] + " ";
                }
                IList<ArrayList> l = helper.ExecuteReader<ArrayList>(sql + " order by " + order, Render);
                return l.Count == 0 ? null : l;
            }
            else
                return null;
        }

        /// <summary>
        /// Sel
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <param name="condition">条件[=/<>]</param>
        /// <param name="order">排序:ID</param>
        /// <returns></returns>
        public IList<ArrayList> Sel(ArrayList field, ArrayList data, ArrayList condition, String order)
        {
            String sql = "select * from [" + tableName + "] where ";
            if (field.Count == data.Count)
            {
                for (int i = 0; i < field.Count; i++)
                {
                    String type = data[i].GetType().Name;
                    if (type == "UINT32" || type == "Int32")
                        sql += field[i] + " " + condition[i] + " " + data[i];
                    else
                        sql += field[i] + " " + condition[i] + " '" + data[i] + "'";
                    if (i + 1 != field.Count)
                        sql += " and ";
                }
                IList<ArrayList> l = helper.ExecuteReader<ArrayList>(sql + " order by " + order, Render);
                return l.Count == 0 ? null : l;
            }
            else
                return null;
        }

        public DataTable Sel(Int32 topCount, String order)
        {
            String sql = SqlPage(topCount) + " order by " + order;
            return helper.ExecuteReader(sql).Tables[0];
        }

        /// <summary>
        /// SelPage
        /// </summary>
        /// <param name="column">条件列</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前页数</param>
        /// <returns></returns>
        public DataTable SelPage(out Int32 count, String column, Int32 pageSize, Int32 pageIndex)
        {
            String sql = SqlPage(pageSize);
            count = SelCount();
            if (pageIndex != 1)
            {
                sql += " where " + column + " not in ( ";
                sql += "select top " + (pageSize * (pageIndex - 1)) + " " + column + " from " + tableName;
                sql += OrderBy();
                sql += " ) ";
            }
            sql += OrderBy();
            return helper.ExecuteReader(sql).Tables[0];
        }

        /// <summary>
        /// 结果行数
        /// </summary>
        /// <param name="filed">字段</param>
        /// <param name="data">数据</param>
        /// <param name="order">排序</param>
        /// <returns>数据</returns>
        public int SelCount(string filed, string data, string order)
        {
            string sql = "select * from [" + tableName + "] where [" + filed + "] = '" + data + "' order by " + order;
            return helper.ExecuteReader<ArrayList>(sql, Render).Count;
        }

        /// <summary>
        /// 添加排序
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public String OrderBy()
        {
            try
            {
                return " order by " + orderBy;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region Insert

        /// <summary>
        /// 插入数据
        /// 自动生成Int 类型 ID
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>影响行数</returns>
        public int Insert(ArrayList data)
        {
            string sql = GetInsertSql(data).Replace("{0}", (GetCount() + 1).ToString());
            return helper.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 插入数据
        /// 不自动生成ID
        /// </summary>
        /// <param name="dataList"></param>
        /// <returns></returns>
        public int Insert2(ArrayList data)
        {
            string sql = GetInsertSql(data).Replace("{0},", "");
            try
            {
                return helper.ExecuteNonQuery(sql);
            }
            catch (Exception e)
            {
                string msg = e.Message;
                throw;
            }
        }

        /// <summary>
        /// 插入数据
        /// 自动生成Guid
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>影响行数</returns>
        public int Insert3(ArrayList data)
        {
            string sql = GetInsertSql(data).Replace("{0}", "'" + Guid.NewGuid().ToString() + "'");
            try
            {
                return helper.ExecuteNonQuery(sql);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取InsertSql语句
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected string GetInsertSql(ArrayList data)
        {
            string sql = "insert into [" + tableName + "] values ({0}";
            for (int i = 0; i < data.Count; i++)
            {
                sql += "," + data[i].GetData().Replace("=", "").Replace("is", "");
            }
            return sql + ")";
        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除内容
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public int Del(string where, string data)
        {
            string sql = "delete from [" + tableName + "] where [" + where + "] = '" + data + "'";
            return helper.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public int Del(String where, Int32 data)
        {
            string sql = "delete from [" + tableName + "] where [" + where + "] = " + data + "";
            return helper.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Del()
        {
            string sql = "delete from [" + tableName + "]";
            return helper.ExecuteNonQuery(sql);
        }
        #endregion

        #region Update
        /// <summary>
        /// UPdate
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <param name="where"></param>
        /// <param name="whereField"></param>
        /// <returns></returns>
        public int Update(ArrayList field, ArrayList data, string where, string whereField)
        {
            string sql = "update " + tableName + " set ";
            for (int i = 0; i < field.Count; i++)
            {
                String type = data[i].GetType().Name;
                if (type == "UINT32" || type == "Int32")
                    sql += field[i] + " = " + data[i];
                else
                    sql += field[i] + " = '" + data[i] + "'";
                if (i + 1 != field.Count)
                    sql += ", ";
            }
            return helper.ExecuteNonQuery(sql + " where " + where + " = '" + whereField + "'");
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public int Update(ArrayList field, ArrayList data)
        {
            string sql = "update " + tableName + " set ";
            sql += GetUpdateSetSql(field, data);
            return helper.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 拼接Update Sql语句
        /// </summary>
        /// <returns></returns>
        protected String UpdateSql()
        {
            return "update " + tableName;
        }

        /// <summary>
        /// 拼接Update Sql语句
        /// </summary>
        /// <returns></returns>
        protected String UpdateSql(string field)
        {
            return UpdateSql() + " set [" + field + "] ";
        }


        protected string GetUpdateSetSql(ArrayList field, ArrayList data)
        {
            string sql = string.Empty;
            for (int i = 0; i < field.Count; i++)
            {
                sql += field[i] + " " + data[i].GetData();
                if (i + 1 != field.Count) sql += ", ";
            }
            return sql;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="field">更新的字段</param>
        /// <param name="data">更新的数据</param>
        /// <param name="where">条件字段</param>
        /// <param name="prove">条件字段数据</param>
        /// <returns>影响行数</returns>
        public Int32 Update(String field, object data, String where, object prove)
        {
            String sql = UpdateSql() + " set [" + field + "] " + data.GetData() + " where [" + where + "] " + prove.GetData() + ";";
            return helper.ExecuteNonQuery(sql);
        }

        public Int32 Update(String field, object data)
        {
            String sql = UpdateSql(field) + data.GetData() + ";";
            return helper.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="field"></param>
        /// <param name="data"></param>
        /// <param name="where"></param>
        /// <param name="prove"></param>
        /// <returns></returns>
        public int Update(ArrayList field, ArrayList data, string where, object prove)
        {
            string sql = "update " + tableName + " set ";
            for (int i = 0; i < field.Count; i++)
            {
                sql += field[i] + data[i].GetData();
                if (i + 1 != field.Count)
                {
                    sql += ", ";
                }
            }
            return helper.ExecuteNonQuery(sql + " where " + where + prove.GetData() + "");
        }
        #endregion

        #region Execute
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExecuteNonQuery(string sql)
        {
            return helper.ExecuteNonQuery(sql);
        }

        public IList<T> ExecuteReader<T>(string sql, IDataRender<T> Reader)
        {
            return helper.ExecuteReader<T>(sql, Reader);
        }
        #endregion

        #region Alter
        /// <summary>
        /// 增加表中的字段
        /// </summary>
        /// <param name="column">字段</param>
        public void AddColumn(string column, SQLDataType type)
        {
            string sql = "ALTER TABLE [" + tableName + "] add COLUMN [" + column + "] " + type.ToString();
            helper.ExecuteNonQuery(sql);
        }
        #endregion

        #region Create
        /// <summary>
        /// 添加表
        /// 默认columnList中第一个元素为主键
        /// </summary>
        /// <param name="columnList"></param>
        public void AddTable(List<ColumnModel> columnList)
        {
            string sql = "create table [" + tableName + "] (" + columnList[0].ToSQL() + " PRIMARY KEY";
            if (columnList.Count == 1)
            {
                sql += ")";
            }
            else
            {
                sql += ",";
                for (int i = 1; i < columnList.Count; i++)
                {
                    if ((i + 1) == columnList.Count)
                    {
                        sql += columnList[i].ToSQL() + ");";
                    }
                    else
                    {
                        sql += columnList[i].ToSQL() + ",";
                    }
                }
            }
            helper.ExecuteNonQuery(sql);
        }
        #endregion
    }
    #endregion

    #region ColumnModel 字段属性类
    /// <summary>
    /// 字段属性类
    /// </summary>
    public class ColumnModel
    {
        public ColumnModel()
        {

        }

        public ColumnModel(string name, SQLDataType type, int length)
        {
            this.ColumnName = name;
            this.ColumnType = type;
            this.ColumnLength = length;
        }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 字段类型
        /// </summary>
        public SQLDataType ColumnType { get; set; }

        /// <summary>
        /// 字段长度
        /// </summary>
        public int ColumnLength { get; set; }

        /// <summary>
        /// 模板
        /// </summary>
        protected readonly string template = "[{0}] {1}";

        public string ToSQL()
        {
            if (ColumnLength == 0 || ColumnLength == null || ColumnType == SQLDataType.Integer)
            {
                return template.Replace("{0}", ColumnName).Replace("{1}", ColumnType.ToString());
            }
            return "[" + ColumnName + "] " + ColumnType.ToString() + "(" + ColumnLength + ")";
        }
    }
    #endregion
}
